package manage.view;


import manage.model.Student;
import manage.utils.SqliteDb;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ModifyView extends JFrame {
    JTextField id, name, age, gender, classes, score, phone;

    // 构造方法中绘制页面
    public ModifyView(Student student) {
        add(north(student), BorderLayout.NORTH);
        add(south(), BorderLayout.SOUTH);

        setTitle("修改学生");
        setBounds(250, 150, 400, 300);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    // 北边：设置学生的属性，每个属性一个标签，一个输入框（定义成属性）
    private JPanel north(Student student) {
        JPanel n = new JPanel();
        JLabel z = new JLabel("编号");
        // 添加到panel上
        n.add(z);
        id = new JTextField(String.valueOf(student.getId()));
        id.setEditable(false);
        n.add(id);

        // 创建一个label
        JLabel a = new JLabel("姓名");
        // 添加到panel上
        n.add(a);
        name = new JTextField(student.getName());
        n.add(name);

        JLabel b = new JLabel("年龄");
        n.add(b);
        age = new JTextField(student.getAge());
        n.add(age);

        JLabel c = new JLabel("班级");
        n.add(c);
        classes = new JTextField(student.getClasses());
        n.add(classes);

        JLabel d = new JLabel("性别");
        n.add(d);
        gender = new JTextField(student.getGender());
        n.add(gender);

        JLabel e = new JLabel("学分");
        n.add(e);
        score = new JTextField(student.getScore());
        n.add(score);

        JLabel f = new JLabel("联系方式");
        n.add(f);
        phone = new JTextField(student.getPhone());
        n.add(phone);

        GridLayout gridLayout = new GridLayout(7, 2);
        n.setLayout(gridLayout);

        return n;
    }

    // 南边：确定、取消两个按钮
    private JPanel south() {
        JPanel s = new JPanel();

        JButton ok = new JButton("确定");
        ok.addActionListener(new OkAction());
        s.add(ok);

        JButton cancel = new JButton("取消");
        cancel.addActionListener(new CancelAction());
        s.add(cancel);

        return s;
    }

    // 确定按钮的功能
    public class OkAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // 获取界面的输入
            String z = id.getText();
            String a = name.getText();
            String b = age.getText();
            String c = gender.getText();
            String d = classes.getText();
            String f = score.getText();
            String g = phone.getText();
            // 必填参数检查
            if (a == null || a.equals("")) {
                JOptionPane.showMessageDialog(null, "名字不能为空");
                return;
            }

            // 根据界面的输入构造学生对象
            // 给构造方法传参，要与定义的参数一致
            // int id, String name, String age, String gender, String classes, String phone, String score
            Student student = new Student(Integer.parseInt(z), a, b, c, d, g, f);
            // 调用写数据库的方法写入数据库
            boolean bool = SqliteDb.modifyStudent(student);
            // 刷新表格
            if (bool == true) {
                JOptionPane.showMessageDialog(null, "修改成功");
                // MainView 中的 jTable改成静态的属性，用static修饰
                MainView.refreshTable(MainView.jTable, SqliteDb.queryStudent());
                // 关闭页面
                dispose();
            }
        }
    }

    // 取消按钮的功能
    public class CancelAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            dispose(); // 关闭页面
        }
    }

}
